package com.onLineCar.netty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.onLineCar.common.system.domain.SysUser;
import com.onLineCar.netty.mapper.NettyMapper;
import com.onLineCar.netty.service.NettyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: onLineCar-server
 * @description:
 * @author: godGuang
 * @create: 2023-12-14 11:01
 **/
@Service
public class NettyServiceImpl implements NettyService {
    @Value("${netty.url}")
    private String url;

    private final String PATH = "D://data/";

    @Autowired
    private NettyMapper nettyMapper;


    @Override
    public String getWebSocketServerUrl() {
        return url;
    }

    @Override
    public List<SysUser> getFriendList(String userName) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(SysUser::getUserId, SysUser::getUserName, SysUser::getPassword, SysUser::getAvatar, SysUser::getNickName)
                .ne(SysUser::getUserName, userName);
        return nettyMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> uploadFile(MultipartFile file) {
        long fileSize = file.getSize();
        String fileName = file.getOriginalFilename(); // 文件名字.
        String FileContentType = file.getContentType(); // 文件类型
        try {
            File TargetFile = new File(PATH);
            if (!TargetFile.exists()) {
                TargetFile.mkdirs();
            }
            if (!FileContentType.equals("")) {
                // 输出文件到目录
                FileOutputStream out = new FileOutputStream(PATH + fileName);
                out.write(file.getBytes());
                out.flush();
                out.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("上传失败");
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("fileName", fileName);
        resultMap.put("fileType", FileContentType);
        resultMap.put("fileSize", fileSize);
        resultMap.put("filePath", PATH);
        return resultMap;
    }

    @Override
    public void download(String fileName, HttpServletResponse response) throws IOException {
        InputStream inStream = null;
        final int size = 1024;
        final byte[] b = new byte[size];
        int len = 0;
        try {
            inStream = new FileInputStream(PATH + URLEncoder.encode(fileName, "iso8859-1"));
            // 设置输出的格式
            response.reset();
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            // 写出
            while ((len = inStream.read(b)) > 0) {
                response.getOutputStream().write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭资源
                inStream.close();
                response.getOutputStream().flush();
                response.getOutputStream().close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}

